import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index'
import {auth} from "@/utils/auth";

Vue.use(Router)
// while = ['']
// bef(()=>{
//   path
// })
// const routers = []
// const router = new Router({})
// export default router
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
const wList = ['/login']

const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: ()=> import('@/view/login/index')
    },
    {
      path: '/',
      name: 'index',
      redirect:'configuration'
      // component: ()=> import('@/view/configuration/index')
    },
    {
      path: '/ele',
      name: 'ele',
      component: ()=> import('@/view/ele/index')
    },
    {
      path: '/yanwu',
      name: 'yanwu',
      component: () => import('@/view/yanwu/yanwu')
    },
    {
      path: '/radio',
      name: 'radio',
      component: ()=> import('@/view/radio/index')
    },
    {
      path: '/tah',
      name: 'tah',
      component: ()=> import('@/view/tandh/index')
    },
    {
      path: '/ele_detail',
      name: 'ele_detail',
      component: ()=> import('@/view/ele/detail')
    },
    {
      path: '/tandh_detail',
      name: 'tandh_detail',
      component: ()=> import('@/view/tandh/detail')
    },
    {
      path: '/warn',
      name: 'warn',
      component: ()=> import('@/view/warn/index')
    },
    {
      path: '/voltage',
      name: 'voltage',
      component: ()=> import('@/view/voltage/index')
    },
    {
      path: '/configuration',
      name: 'configuration',
      component: ()=> import('@/view/configuration/index')
    },
    {
      path: '/set',
      name: 'set',
      component: ()=> import('@/view/set/index')
    },
    {
      path: '/meterread',
      name: 'meterread',
      component: ()=> import('@/view/nhjiance/meterread')
    },
    // {
    //   path: '/meterread',
    //   name: 'meterread',
    //   component: ()=> import('@/view/set/meterread')
    // },
    {
      path: '/realtime',
      name: 'realtime',
      component: ()=> import('@/view/nhjiance/realtime')
    },
    // {
    //   path: '/realtime',
    //   name: 'realtime',
    //   component: ()=> import('@/view/set/realtime')
    // },
    {
      path: '/limit',
      name: 'limit',
      component: ()=> import('@/view/set/limit')
    },
    {
      path: '/organization',
      name: 'organization',
      component: ()=> import('@/view/set/organization')
    },
    
    {
      path: '*',//匹配未定义的路由
      redirect: '/'//重定向
  },
  ]
})

router.beforeEach((to, from, next) => {
  let token = auth.get("token")
  // console.log('shouwei',token)
  // console.log('to', wList.indexOf(to.fullPath))
  if(wList.indexOf(to.fullPath) == -1) {
    if(token){
      next();
    }else{
      next({
        'name': 'login'
      })
    }
  }else{
    next();
  }
});

export default router